﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business;
using System.Collections;
using Telerik.Web.UI;
using System.Drawing;
using System.Globalization;

namespace BVVD_Website.ql_danh_muc
{
    public partial class frmDanhmuc2 : System.Web.UI.Page
    {
        public static string mfrm = "MNU017";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BVVDDataContext db = new BVVDDataContext();
                if (Session["user"] != null)
                {
                    LST_NSD user = Session["user"] as LST_NSD;

                    V_PHANQUYEN pq = (from p in db.V_PHANQUYENs
                                      where p.MAMN.Equals(mfrm) && p.MANSD.Equals(user.MANSD)
                                      select p).SingleOrDefault();
                    if (pq != null)
                    {
                        grvDanhMuc.MasterTableView.CommandItemSettings.ShowAddNewRecordButton = pq.ADD > 0 ? true : false;
                        grvDanhMuc.MasterTableView.Columns.FindByUniqueName("btnDelete").Display = pq.DELETE > 0 ? true : false;
                        grvDanhMuc.MasterTableView.Columns.FindByUniqueName("btnEdit").Display = pq.EDIT > 0 ? true : false;
                    }
                }
                grvDanhMuc.Culture = new CultureInfo("vi-VN");
                grvDanhMuc.Rebind();


                ddlLOAIDM.DataSource = (from loaidm in db.LST_LOAIDMs
                                        where loaidm.TRANGTHAI.Equals("1")
                                        select loaidm).ToList();
                ddlLOAIDM.DataTextField = "TENLOAIDM";
                ddlLOAIDM.DataValueField = "MALOAIDM";
                ddlLOAIDM.DataBind();

                fillgrvDanhMuc();

                if (ddlLOAIDM.SelectedIndex >= 0)
                {
                    lblMenu.Text = "DANH SÁCH " + ddlLOAIDM.SelectedText.ToUpper();
                }
                else
                {
                    lblMenu.Text = "DANH SÁCH CÁC LOẠI DANH MỤC";
                }
                grvDanhMuc.DataBind();

            }
        }
        public List<LST_DANHMUC> getData()
        {
            BVVDDataStore<LST_DANHMUC> db = new BVVDDataStore<LST_DANHMUC>();
            Hashtable condition = new Hashtable();
            condition.Add("MALOAIDM", ddlLOAIDM.SelectedValue);
            return db.getFromDB(condition);
        }

        private void fillgrvDanhMuc()
        {
            BVVDDataContext db = new BVVDDataContext();
            var list = (from dm in db.LST_DANHMUCs

                        select new
                        {
                            RECID = dm.RECID,
                            MALOAIDM = dm.MALOAIDM,
                            TENLOAIDM = dm.LST_LOAIDM.TENLOAIDM,
                            MADM = dm.MADM,
                            TENDM = dm.TENDM,
                            TRANGTHAI = dm.TRANGTHAI,
                            GHICHU = dm.GHICHU
                        }).ToList();
            if (ddlLOAIDM.SelectedIndex >= 0)
            {
                grvDanhMuc.DataSource = list.Where(a => a.MALOAIDM.Equals(ddlLOAIDM.SelectedValue));

            }
            else
            {
                grvDanhMuc.DataSource = list;

            }

        }

        protected void grvDanhMuc_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {

            fillgrvDanhMuc();

        }
        protected void grvDanhMuc_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            //BVVDDataStore<LST_LOAIDM> type = new BVVDDataStore<LST_LOAIDM>();
            //if (e.Item is GridDataItem)
            //{
            //    GridDataItem dataItem = e.Item as GridDataItem;
            //    if (dataItem["TRANGTHAI"].Text == "0")
            //        dataItem.ForeColor = Color.Silver;
            //}
            if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
            {
                GridEditFormItem editFormItem = e.Item as GridEditFormItem;
                GridDataItem parentItem = editFormItem.ParentItem;

                LinkButton btnUpdate = editFormItem.FindControl("btnUpdate") as LinkButton;

                Label lblLOAIDM = editFormItem.FindControl("lblLOAIDM") as Label;

                RadioButtonList rblTRANGTHAI = editFormItem.FindControl("rblTRANGTHAI") as RadioButtonList;

                if (e.Item is GridEditFormInsertItem || e.Item is GridDataInsertItem)
                {
                    lblLOAIDM.Text = ddlLOAIDM.SelectedText;
                }
                else
                {
                    Label lblTRANGTHAI = editFormItem["TRANGTHAI"].FindControl("lblTRANGTHAI") as Label;
                    lblLOAIDM.Text = ddlLOAIDM.SelectedText;
                    rblTRANGTHAI.SelectedValue = lblTRANGTHAI.Text.Equals("ACTIVE") ? "1" : "0";
                    if (!grvDanhMuc.MasterTableView.Columns.FindByUniqueName("btnEdit").Display) {
                        btnUpdate.Enabled = false;
                    }
                }
                ddlLOAIDM.DataBind();
            }
        }
        protected void grvDanhMuc_InsertCommand(object sender, GridCommandEventArgs e)
        {
            GridEditFormInsertItem insertedItem = (GridEditFormInsertItem)e.Item;

            LST_DANHMUC danhmuc = new LST_DANHMUC
            {
                MALOAIDM = ddlLOAIDM.SelectedValue,
                MADM = (insertedItem.FindControl("txtMADM") as RadTextBox).Text,
                TENDM = (insertedItem.FindControl("txtTENDM") as RadTextBox).Text,
                GHICHU = (insertedItem.FindControl("txtGHICHU") as RadTextBox).Text,
                TRANGTHAI = (insertedItem.FindControl("rblTRANGTHAI") as RadioButtonList).SelectedValue
            };
            BVVDDataContext db = new BVVDDataContext();
            db.LST_DANHMUCs.InsertOnSubmit(danhmuc);
            try
            {
                db.SubmitChanges();
                rnAlert.Text = "Thêm mới danh mục <font color='red'> " + danhmuc.TENDM + "</font> thành công ";
            }
            catch (Exception ex)
            {
                rnAlert.Text = "Có lỗi xảy ra \n <br> Nguyên nhân : " + ex.Message;

            }


            rnAlert.Show();
        }
        protected void grvDanhMuc_UpdateCommand(object sender, GridCommandEventArgs e)
        {
            BVVDDataContext db = new BVVDDataContext();
            GridEditableItem editedItem = e.Item as GridEditableItem;
            string recid = editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["RECID"].ToString();

            LST_DANHMUC danhmuc = (from item in db.LST_DANHMUCs
                                   where item.RECID.ToString().Equals(recid)
                                   select item).SingleOrDefault();
            //danhmuc.MALOAIDM = (editedItem.FindControl("ddlLOAIDM") as RadDropDownList).SelectedValue;
            danhmuc.MADM = (editedItem.FindControl("txtMADM") as RadTextBox).Text;
            danhmuc.TENDM = (editedItem.FindControl("txtTENDM") as RadTextBox).Text;
            danhmuc.GHICHU = (editedItem.FindControl("txtGHICHU") as RadTextBox).Text;
            danhmuc.TRANGTHAI = (editedItem.FindControl("rblTRANGTHAI") as RadioButtonList).SelectedValue;
            try
            {
                db.SubmitChanges();
                rnAlert.Text = "Cập nhật thành công danh mục có mã <font color='red'> " + danhmuc.MADM + "</font>";
            }
            catch (Exception ex)
            {
                rnAlert.Text = "Có lỗi xảy ra. \n <br> Nguyên nhân : " + ex.Message;
            }

            rnAlert.Show();

        }
        protected void grvDanhMuc_DeleteCommand(object sender, GridCommandEventArgs e)
        {
            GridDataItem item = (GridDataItem)e.Item;
            string recid = item.OwnerTableView.DataKeyValues[item.ItemIndex]["RECID"].ToString();
            BVVDDataContext db = new BVVDDataContext();
            LST_DANHMUC danhmuc = (from dm in db.LST_DANHMUCs
                                   where dm.RECID.ToString().Equals(recid)
                                   select dm).SingleOrDefault();
            string delDM = "Xóa thành công danh mục có mã <font color='red'> " + danhmuc.MADM + "</font>";
            db.LST_DANHMUCs.DeleteOnSubmit(danhmuc);
            try
            {
                db.SubmitChanges();
                rnAlert.Text = delDM;
            }
            catch (Exception ex)
            {
                rnAlert.Text = "Có lỗi xảy ra. \n <br> Nguyên nhân : " + ex.Message;
            }

            rnAlert.Show();
        }
        protected void ddlLOAIDM_SelectedIndexChanged(object sender, DropDownListEventArgs e)
        {


            if (ddlLOAIDM.SelectedIndex >= 0)
            {
                lblMenu.Text = "DANH SÁCH " + ddlLOAIDM.SelectedText.ToUpper();
            }
            fillgrvDanhMuc();
            grvDanhMuc.DataBind();
        }

        protected void grvDanhMuc_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == RadGrid.InitInsertCommandName)
            {
                e.Canceled = true;
                System.Collections.Specialized.ListDictionary newValues = new System.Collections.Specialized.ListDictionary();
                newValues["MADM"] = "";
                newValues["TENDM"] = "";
                newValues["GHICHU"] = "";
                //e.Item.OwnerTableView.FindControl("rbiPICTURE");
                e.Item.OwnerTableView.InsertItem(newValues);
            }
        }


    }
}